package cn.itcast.controller;

import cn.itcast.common.R;
import cn.itcast.dto.DishDto;
import cn.itcast.entity.ShoppingCart;
import cn.itcast.service.IShoppingCartService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController extends BaseController {
    @Autowired
    private IShoppingCartService shoppingCartService;


    @PostMapping("add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        Long userId = findIdFromSession();
        shoppingCart.setUserId(userId);
        ShoppingCart RtshoppingCart = shoppingCartService.add(shoppingCart);

        return R.success(RtshoppingCart);
    }

    @PostMapping("sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long userId = findIdFromSession();
        shoppingCart.setUserId(userId);
        ShoppingCart RtshoppingCart = shoppingCartService.sub(shoppingCart);

        return R.success(RtshoppingCart);
    }

    @GetMapping("list")
    public R<List<ShoppingCart>> list(){
       List<ShoppingCart> list =shoppingCartService.list(findIdFromSession());
        return R.success(list);
    }

    @DeleteMapping("clean")
    public R<String> clean(){
        shoppingCartService.clean(findIdFromSession());

        return R.success("购物车已清空");
    }

}
